package com.crm.system.controller.trainingManager;

import com.alibaba.fastjson.JSONObject;
import com.crm.common.model.DataResponse;
import com.crm.common.model.DhPageInfo;
import com.crm.common.model.DhResponse;
import com.crm.common.model.Enum.ResponseCodeEnum;
import com.crm.model.entity.internalTrainingManager.NxContact;
import com.crm.model.qo.trainingManager.TrainContractAddQO;
import com.crm.model.qo.trainingManager.TrainingContactListQO;
import com.crm.model.vo.trainingManager.TrainingContractVO;
import com.crm.model.vo.trainingManager.TrainingOrderProductDetailVO;
import com.crm.system.service.trainingManager.TrainingContractService;
import com.crm.system.service.trainingManager.TrainingOrderProductService;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * 内训合同
 * @author luojie
 * @date 2021/9/13 14:09
 */
@RestController
@RequestMapping("/trainingContract")
@Slf4j
public class TrainingContractController {

    @Autowired
    private TrainingContractService trainingContractService;

    @Autowired
    private TrainingOrderProductService trainingOrderProductService;

    /**
     * @Description:添加内训合同基本信息
     * @Param:  * @param body 合同以及相关信息参数
     * @Author: luojie
     * @Date: 2021/3/17 14:08
     */
    @PostMapping("/addTrainingContract")
    public DataResponse addTrainingContract(@RequestBody TrainContractAddQO trainContractAddQO){
        //客户id为空直接返回
        if(StringUtils.isBlank(trainContractAddQO.getCustomerId())||StringUtils.isBlank(trainContractAddQO.getContactId()
        )||StringUtils.isBlank(trainContractAddQO.getContactDetails())){
            return new DhResponse(ResponseCodeEnum.PARAMETER_ERROR.getResponseCode());
        }

        Boolean result = trainingContractService.addTrainingContract(trainContractAddQO);
        if(result){
            return new DhResponse(ResponseCodeEnum.OK.getResponseCode());
        }else {
            return new DhResponse(ResponseCodeEnum.ERROR.getResponseCode());
        }
    }

    /***
     * @Description:获取登录人的内训客户列表
     * @Param:
     * @Author: luojie
     * @Date: 2021/9/14 15:04
     */
    @GetMapping("/getTrainingCustomerList")
    public DataResponse getTrainingCustomerList(){
        List<JSONObject> trainingCustomerList = trainingContractService.getTrainingCustomerList();
        return new DhResponse(ResponseCodeEnum.OK.getResponseCode(),trainingCustomerList);
    }

    /***
     * @Description:根据客户id获取内训联系人
     * @Param:customerId 客户id
     * @Author: luojie
     * @Date: 2021/9/14 15:30
     */
    @GetMapping("/getTrainingCustomerContactList")
    public DataResponse getTrainingCustomerContactList(@RequestParam(value = "customerId", required = true) String customerId){
        List<JSONObject> trainingCustomerContactList = trainingContractService.getTrainingCustomerContactList(customerId);
        return new DhResponse(ResponseCodeEnum.OK.getResponseCode(),trainingCustomerContactList);
    }

    /**
     * (用户模块)内训管理-内训合同列表
     * @author yincuimei
     * @return
     */
    @PostMapping("/selectTrainingContractList")
    public DataResponse selectTrainingContractList(@RequestParam(value = "pageIndex", required = true) Integer pageIndex,//页码
                                               @RequestParam(value = "pageSize", required = true) Integer pageSize,//分页大小
                                               @RequestParam(value="sortName",required = false) String sortName,//排序列名字
                                               @RequestParam(value="sortOrder",required = false) String sortOrder,//排序规则(asc/desc)
                                               TrainingContactListQO trainingContactListQO){
        DhPageInfo<TrainingContractVO> trainingContractVOList = trainingContractService.selectTrainingContractVOList(trainingContactListQO,pageIndex,pageSize,sortName,sortOrder);
        return new DhResponse(ResponseCodeEnum.OK.getResponseCode(),trainingContractVOList);
    }

    /**
     * (用户模块)内训管理-内训合同列表-》合同详情(产品信息)
     * @author yincuimei
     * @return
     */
    @PostMapping("/selectTrainingOrderProductList")
    public DataResponse selectTrainingOrderProductList(@RequestParam(value = "pageIndex", required = true) Integer pageIndex,//页码
                                                       @RequestParam(value = "pageSize", required = true) Integer pageSize,//分页大小
                                                       @RequestParam(value="sortName",required = false) String sortName,//排序列名字
                                                       @RequestParam(value="sortOrder",required = false) String sortOrder,//排序规则(asc/desc)
                                                       @RequestParam(value = "trainingContractId", required = true) String trainingContractId){
        DhPageInfo<TrainingOrderProductDetailVO> trainingOrderProductList = trainingOrderProductService.selectTrainingOrderProductVOList(trainingContractId,pageIndex,pageSize,sortName,sortOrder);
        return new DhResponse(ResponseCodeEnum.OK.getResponseCode(),trainingOrderProductList);
    }
}
